package com.sanrenzu.everybodytv.ui.recommenditent;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.sanrenzu.everybodytv.R;
import com.sanrenzu.everybodytv.bean.sms.IntentBean;
import com.sanrenzu.everybodytv.ui.recommend_live.VideoViewActivity;
import com.sanrenzu.everybodytv.ui.recommenditent.adapter.RecommendIntentGirdViewAdapter;
import com.sanrenzu.everybodytv.widget.MyGirdView;
import com.sanrenzu.everybodytv.widget.VerticalScrollView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class RecommendIntentActivity extends AppCompatActivity implements RecommendIntentContract.View {

    @BindView(R.id.activity_columitem_back)
    ImageView activityColumitemBack;
    @BindView(R.id.activity_columitem_title)
    TextView activityColumitemTitle;
    @BindView(R.id.activity_columitem_gridbview)
    MyGirdView activityColumitemGridbview;
    @BindView(R.id.activity_columitem_presh)
    VerticalScrollView activityColumitemPresh;
    private Unbinder bind;
    //声明上下文对象
    private Context mContext = this;
    //声明传入的字符串
    private String classification;
    private RecommendIntentContract.Presenter presenter;
    //声明标题
    private String title;
    private List<IntentBean.DataBean> list =new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_colum_item);
        bind = ButterKnife.bind(this);
        initData();
        initView();
        initOther();
    }

    private void initOther() {
        activityColumitemGridbview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mContext, VideoViewActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("url",list.get(position).getUid());
                intent.getExtras();
                mContext.startActivity(intent);
            }
        });
    }

    private void initData() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        classification = bundle.getString("classification");
        title = bundle.getString("subtitle");
        activityColumitemTitle.setText(title);
        presenter = new RecommendIntentPresenter(this);
        presenter.getRecommendIntent(classification);
    }

    private void initView() {
        activityColumitemPresh.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        activityColumitemPresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                presenter.getRecommendIntent(classification);
                activityColumitemPresh.onRefreshComplete();
            }
        });
    }

    @Override
    public void onGetRecommendSuccess(IntentBean bean) {
        list = bean.getData();
        RecommendIntentGirdViewAdapter adapter = new RecommendIntentGirdViewAdapter(mContext,list);
        activityColumitemGridbview.setAdapter(adapter);
    }

    @Override
    public void onGetRecommenddFail(String erro) {
        Toast.makeText(mContext,erro,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }
}
